package cn.liccshop.sys.utils;

import cn.liccshop.core.utils.ObjectToOtherUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author LiCC
 * @program LiCCShop
 * @description 日期数字获取
 * @since 2019-05-27-00:50
 */
public class DateNumberUtil {
    public static String getYear(){
        Calendar cal = Calendar.getInstance();
        return ((Integer)cal.get(Calendar.YEAR)).toString();
    }
    public static String getMonth(){
        Calendar cal = Calendar.getInstance();
        Integer month =  cal.get(Calendar.MONTH) + 1;
        return month<10?('0'+month.toString()):month.toString();
    }
    public static String getDay(){
        Calendar cal = Calendar.getInstance();
        return ((Integer)cal.get(Calendar.DATE)).toString();
    }

    /**
     * “yyyy-MM-dd HH:mm:ss”类型字符串装换为yyyy:mm
     * @return
     */
    public static String localDateTimeToYearMonth(LocalDateTime localDateTime){
        int year = localDateTime.getYear();
        int month = localDateTime.getMonthValue();
        return year+":"+(month<10?"0"+month:month);
    }
    public static List<String> betweenYearMonth(String beginDate, String endDate){
        String[] begin = beginDate.split("-");
        String[] end = endDate.split("-");
        Integer beginYear = Integer.valueOf(begin[0]);
        Integer beginMonth = Integer.valueOf(begin[1]);
        Integer endYear = Integer.valueOf(end[0]);
        Integer endMonth = Integer.valueOf(end[1]);
        List<String> data = new ArrayList<>();
        if (beginYear.intValue()<endYear){
            /** 不是同一年 **/
            do {
                data.add(beginYear+":"+((beginMonth<10)?("0"+beginMonth):beginMonth));
                if (beginMonth.intValue() == 12){
                    beginYear += 1;
                    beginMonth = 1;
                } else {
                    beginMonth += 1;
                }
            } while (beginYear.intValue()<endYear.intValue());
            do {
                data.add(beginYear+":"+((beginMonth<10)?("0"+beginMonth):beginMonth));
                beginMonth += 1;
            }while (endMonth.intValue()>=beginMonth.intValue());
        } else {
            /** 同一年 **/
            do {
                data.add(beginYear+":"+((beginMonth<10)?("0"+beginMonth):beginMonth));
                beginMonth += 1;
            }while (endMonth.intValue()>=beginMonth.intValue());
        }
        return data;
    }
}
